package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.*;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IAuthenticationFacade;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ISpecialityService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Teacher")
@Slf4j
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private ISpecialityService specialityService;
    @Autowired
    private IAuthenticationFacade authenticationFacade;

    @RequestMapping(value = {"/toAddTeacher"})
    public String toAddTeacher(Model model) {
        List<Speciality> specialities = specialityService.queryAllSpeciality();
        model.addAttribute("specialities",specialities);
        return "toaddteacher";
    }

    @RequestMapping(value = {"/toUpdateTeacher"})
    public ModelAndView toUpdateTeacher(Integer id) {
        List<Speciality> specialities = specialityService.queryAllSpeciality();
        Teacher teacher = teacherService.queryTeacherById(id);
        ModelAndView model = new ModelAndView("toupdateteacher");
        model.addObject("teacher", teacher);
        model.addObject("specialities", specialities);
        return model;
    }

    @RequestMapping("queryAllTeacher")
    public String queryAllTeacher(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                  @RequestParam(value = "pageSize",defaultValue = "5")int pageSize)
    {
        int totalCount = teacherService.queryTeacherCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);

        List<Map<String,Object>> teachers = new ArrayList<Map<String,Object>>();
        List<Teacher> teachers_ = teacherService.queryTeacherByPage(pageNow,pageSize);

        if(teachers_ != null && teachers_.size() > 0){
            for (Teacher teacher_ : teachers_) {
                Map<String,Object> teacher = new HashMap<>();
                Speciality speciality = specialityService.querySpecialityById(teacher_.getSpecialityId());
                teacher.put("id",teacher_.getId());
                teacher.put("name",teacher_.getName());
                teacher.put("specialityname",speciality.getName());
                teacher.put("employeenum",teacher_.getEmployeenum());
                teacher.put("teachernum",teacher_.getTeachernum());
                teacher.put("mobile",teacher_.getMobile());
                teacher.put("username",teacher_.getUsername());
                teacher.put("password",teacher_.getPassword());
                teacher.put("ispresent",teacher_.getIsPresent());
                teachers.add(teacher);
            }
        }

        Authentication authentication = authenticationFacade.getAuthentication();
        String currentTeacherName = authentication.getName();
        model.addAttribute("teachers", teachers);
        model.addAttribute("teacherName", currentTeacherName);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "teacher";
    }

    @RequestMapping("queryTeacherById")
    @ResponseBody
    public Teacher queryTeacherById(Integer id)
    {
        return teacherService.queryTeacherById(id);
    }

    @PostMapping("insertTeacher")
    @ResponseBody
    public String insertTeacher(Teacher teacher)
    {
//        Teacher teacher = new Teacher(null,null,null,null,null,null,username,password,null);
        teacherService.insertTeacher(teacher);
        return "OK";
    }

    @RequestMapping("doRegister")
    public String doRegister(String username, String password1, String password2, Model model){
        System.out.println("password1 = " + password1 + ",password2 = " + password2);
        log.info("password1 = " + password1 + ",password2 = " + password2);
        if (!password2.equals(password1)) {
            model.addAttribute("msg","两次密码不一致")  ;
            model.addAttribute("username",username);
            model.addAttribute("password1",password1);
            model.addAttribute("password2",password2);
            return "/register";
        }else{
            Teacher teacher = new Teacher(null,null,null,null,null,null,username,password1,null);
            int nRet = teacherService.insertTeacher(teacher);

            if(nRet == 1){// 注册成功
                model.addAttribute("username",username);
                return "/login";
            }else if(nRet == -1){// 已存在账号
                model.addAttribute("msg","该账号已存在!")  ;
                return "/register";
            }else{// 注册失败
                model.addAttribute("msg","注册失败!")  ;
                return "/register";
            }
        }
    }

    @RequestMapping("updateTeacherById")
    @ResponseBody
    public String updateTeacherById(Teacher teacher)
    {
        teacherService.updateTeacherById(teacher);
        return "OK";
    }

    @RequestMapping("deleteTeacherById")
    @ResponseBody
    public String deleteTeacherById(Integer id)
    {
        teacherService.deleteTeacherById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("queryTeacherByName")
    public List<Teacher> queryTeacherByName(String name)
    {
        return teacherService.queryTeacherByName(name);
    }
}
